package com.micro.frog.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micro.frog.common.base.PageRes;
import com.micro.frog.common.base.Result;
import com.micro.frog.rbac.dto.RoleDTO;
import com.micro.frog.rbac.entity.RoleDO;
import com.micro.frog.rbac.mapper.RoleMapper;
import com.micro.frog.rbac.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 角色操作
 *
 * @author :  ljh
 * @date : 2018/4/12
 **/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleDO> implements RoleService {

    @Override
    public List<RoleDO> queryList(RoleDO role) {
        QueryWrapper<RoleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(role);
        return this.list(queryWrapper);
    }

    @Override
    public Result<PageRes<RoleDO>> queryPage(RoleDTO query) {
        RoleDO role = new RoleDO();
        BeanUtils.copyProperties(query, role);
        QueryWrapper<RoleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(role);
        IPage<RoleDO> iPage = this.page(new Page<>(query.getPageNum(), query.getPageSize()));
        return Result.success(PageRes.success(iPage.getCurrent(), iPage.getSize(), iPage.getTotal(), iPage.getRecords()));
    }

    @Override
    public boolean add(RoleDO role) {
        role.setIsDeleted("N");
        role.setCreatedTime(new Date());
        return this.save(role);
    }

    @Override
    public boolean delete(Long id) {
        RoleDO role = new RoleDO();
        role.setId(id);
        role.setIsDeleted("Y");
        return this.updateById(role);
    }

    @Override
    public boolean update(RoleDO role) {
        role.setUpdatedTime(new Date());
        return this.updateById(role);
    }
}
